package work.xkong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import work.xkong.bo.LoginBO;
import work.xkong.common.Const;
import work.xkong.common.ResponseRest;
import work.xkong.entity.Student;
import work.xkong.mapper.AdminMapper;
import work.xkong.mapper.StudentMapper;
import work.xkong.mapper.TeacherMapper;
import work.xkong.service.IAdminService;
import work.xkong.service.IStudentService;
import work.xkong.service.ISystemService;
import work.xkong.service.ITeacherService;
import work.xkong.utils.TokenUtils;
import work.xkong.vo.inputvo.LoginInfoInputVo;
import work.xkong.vo.outputvo.LoginInfoOutputVo;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @PACKAGE_NAME: work.xkong.service.impl
 * @NAME: SystemServiceImpl
 * @USER: NuanYang
 * @author: 查炼飞
 * @create: 2022/11/21 17:04
 */
@Service
public class SystemServiceImpl implements ISystemService {
    @Autowired
    private LoginBO loginBO;
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public ResponseRest doLogin(LoginInfoInputVo loginInfoInput) {

        LoginInfoOutputVo user = loginBO.getUser(loginInfoInput);

        if (user == null) {
            return ResponseRest.creatFail("账号或密码错误，请重新登录");
        }

        if (user.getPassword().equals(loginInfoInput.getPassword())){
            if (loginInfoInput.getType() == Const.LoginInfo.STUDENT){
                Student student = new Student();
                student.setStudentId(user.getUserId());
                student.setStudentLastLoginTime(LocalDateTime.now());
                studentMapper.updateById(student);
            }
            return ResponseRest.creatSuccess("恭喜你，登录成功咯", TokenUtils.getToken(user));
        }

        return ResponseRest.creatFail("账号或密码错误，请重新登录");
    }
}
